﻿@using admin.Model
@{
    ViewData["Title"] = "OrderList";
    Layout = "~/Views/Shared/_SubMenuLayout.cshtml";
    var orders = Model as List<Order>;
}

<title>订单列表</title>
<nav class="breadcrumb"><i class="Hui-iconfont">&#xe67f;</i> 首页 <span class="c-gray en">&gt;</span> 订单管理 <span class="c-gray en">&gt;</span> 订单列表 <a class="btn btn-success radius r" style="line-height:1.6em;margin-top:3px" href="javascript:location.replace(location.href);" title="刷新"><i class="Hui-iconfont">&#xe68f;</i></a></nav>
<div class="page-container">
    <div class="cl pd-5 bg-1 bk-gray mt-20">
        <span class="l">
            <a href="javascript:;" onclick="order_batch_del('/Home/OrderDelB')" class="btn btn-danger radius"><i class="Hui-iconfont">&#xe6e2;</i> 批量删除</a>
            <a href="javascript:;" onclick="order_batch_check('/Home/OrderCheckB')" class="btn btn-primary radius"><i class="Hui-iconfont">&#xe6e1;</i> 批量审核</a>
        </span>
        <span class="r">共有数据：<strong>@orders.Count</strong> 条</span>
    </div>
    <div class="mt-20">
        <table class="table table-border table-bordered table-hover table-bg table-sort" id="usertable">
            <thead>

                <tr class="text-c">
                    <th width="3%"><input type="checkbox" name="" value=""></th>
                    <th width="5%">订单ID</th>
                    <th width="9%">顾客ID</th>
                    <th width="18%">下单时间</th>
                    <th width="14%">书名</th>
                    <th width="6%">数量(/本)</th>
                    <th width="8%">总价(/元)</th>
                    <th width="">地址</th>
                    <th width="8%">订单状态</th>
                    <th width="8%">操作</th>
                </tr>
            </thead>
            <tbody>
                @for (int i = 0; i < orders.Count; i++)
                {
                    <tr class="text-c">
                        <td><input type="checkbox" value="@orders[i].Id" name="orderChk"></td>
                        <td><a title="查看订单信息" onclick="order_edit('编辑此订单','/Home/OrderEdit','550','500','@orders[i].Id')" href="javascript:;">@orders[i].Id</a></td>
                        <td><a title="查看用户信息" onclick="user_show('用户详细信息','/Home/UserShow','450','450','@orders[i].Cid')" href="javascript:;">@orders[i].Cid</a></td>
                        <td>@orders[i].OrderTime</td>
                        <td><a title="查看图书信息" onClick="Book_show_by_name('查看图书信息','/Home/BookShowByName','460','660','@orders[i].Bookname')" href="javascript:;">@orders[i].Bookname</a></td>
                        <td>@orders[i].Quantity</td>
                        <td>@orders[i].TotalPrice</td>
                        <td>@orders[i].Address</td>
                        @if (orders[i].OrderStatus == "等待发货" || orders[i].OrderStatus == "退书中")
                         {
                            <td class="td-status" id="order-status"><span class="label label-danger radius">@orders[i].OrderStatus</span></td>
                         }
                         else if (orders[i].OrderStatus == "已发货" || orders[i].OrderStatus == "已退货" || orders[i].OrderStatus == "已收货")
                         {
                            <td class="td-status" id="order-status"><span class="label label-success radius">@orders[i].OrderStatus</span></td>
                         }
                        <td class="f-14 td-manage" id="td-action">
                            @if (orders[i].OrderStatus == "等待发货" || orders[i].OrderStatus == "退书中")
                            {
                                <a style="text-decoration:none" class="ml-5 del" id="check-action" onClick="order_check(this,'/Home/OrderCheck','@orders[i].Id')" href="javascript:;" title="审核该订单"><i class="Hui-iconfont">&#xe725;</i></a>
                            }
                            else if (orders[i].OrderStatus == "已发货" || orders[i].OrderStatus == "已退货" || orders[i].OrderStatus == "已收货")
                            {
                                <a class="c-primary del" title="已审核"><i class="Hui-iconfont">&#xe6e1;</i></a>
                            }
                            <a title="编辑" href="javascript:;" onclick="order_edit('编辑此订单','/Home/OrderEdit','550','500','@orders[i].Id')" class="ml-5" style="text-decoration:none"><i class="Hui-iconfont">&#xe60c;</i></a>
                            <a title="删除此订单" href="javascript:;" onclick="order_del(this,'/Home/OrderDel','@orders[i].Id')" class="ml-5" style="text-decoration:none"><i class="Hui-iconfont">&#xe6e2;</i></a>
                        </td>
                    </tr>
                }
            </tbody>
        </table>
    </div>
</div>

<script type="text/javascript">
    /*订单-编辑*/
    function order_edit(title, url, w, h, id) {
        url = url + '?id=' + id;
        layer.open({
            type: 2,
            area: [w + 'px', h + 'px'],
            fix: false, //不固定
            maxmin: true,
            shade: 0.4,
            title: title,
            content: url
        });
    }

    /*订单审核*/
    function order_check(obj, ourl, id) {
        var url = ourl + "?id=" + id;
        layer.confirm('审核订单？', {
            btn: ['发货', '退货', '取消'],
            shade: false,
            closeBtn: 0
        },
            function () {
                $.ajax({
                    type: "POST",
                    url: url,
                    data: { "orderstatu": '已发货' },
                    success: function (result) {
                        if (result == "ok") {
                            $(obj).parents("tr").find(".td-manage").prepend('<a class="c-primary" title="已审核"><i class="Hui-iconfont">&#xe6e1;</i></a>');
                            $(obj).parents("tr").find(".td-status").html('<span class="label label-success radius">已发货</span>');
                            $(obj).remove();
                            layer.msg('已发货', { icon: 6, time: 2000 });
                        }
                        else if (result == "false")
                            layer.msg('发货失败！该订单不能发货', { icon: 5, time: 2000 });
                    },
                    error: function () {
                        layer.msg('网络错误！请稍后再试', { icon: 5, time: 2000 });
                    }
                });
            },
            function () {
                $.ajax({
                    type: "POST",
                    url: url,
                    data: { "orderstatu": '已退货' },
                    success: function (result) {
                        if (result == "ok") {
                            $(obj).parents("tr").find(".td-manage").prepend('<a class="c-primary" title="已审核"><i class="Hui-iconfont">&#xe6e1;</i></a>');
                            $(obj).parents("tr").find(".td-status").html('<span class="label label-success radius">已退货</span>');
                            $(obj).remove();
                            layer.msg('已退货', { icon: 6, time: 2000 });
                        }
                        else if (result == "false")
                            layer.msg('退货失败！该订单不能退货', { icon: 5, time: 2000 });
                    },
                    error: function () {
                        layer.msg('网络错误！请稍后再试', { icon: 5, time: 2000 });
                    }
                });
            });
    }
    /*订单批量审核*/
    function order_batch_check(url) {
        var checkedNum = $("input[name='orderChk']:checked").length;
        if (checkedNum == 0) {
            layer.msg('请选择至少一项！', { icon: 6, time: 2000 });
            return;
        }
        layer.confirm('审核订单？', {
            btn: ['发货', '退货', '取消'],
            shade: false,
            closeBtn: 0
        },
            function () {
                var checkedList = new Array();
                checkedList.push('已发货');
                $("input[name='orderChk']:checked").each(function () {
                    checkedList.push($(this).val());
                });
                $.ajax({
                    type: "POST",
                    url: url,
                    data: { 'checkitems': checkedList.toString() },
                    success: function (result) {
                        if (result == "ok") {
                            $("input[name='orderChk']:checked").each(function () {
                                $(this).parents("tr").find(".td-manage").find(".del").remove();
                            });
                            $("[name ='orderChk']:checkbox").attr("checked", false);//清除选项
                            layer.msg('已发货', { icon: 6, time: 2000 });
                            window.location.reload();
                        }
                        else if (result == "false") {
                            $("[name ='orderChk']:checkbox").attr("checked", false);
                            layer.msg('发货失败！存在已不能再发货的订单', { icon: 5, time: 2000 });
                        }
                            
                    },
                    error: function () {
                        layer.msg('网络错误！请稍后再试', { icon: 5, time: 2000 });
                    }
                });
            },
            function () {
                var checkedList = new Array();
                checkedList.push('已退货');
                $("input[name='orderChk']:checked").each(function () {
                    checkedList.push($(this).val());
                });
                $.ajax({
                    type: "POST",
                    url: url,
                    data: { 'checkitems': checkedList.toString() },
                    success: function (result) {
                        if (result == "ok") {
                            $("input[name='orderChk']:checked").each(function () {
                                $(this).parents("tr").find(".td-manage").find(".del").remove();
                            });
                            $("[name ='orderChk']:checkbox").attr("checked", false);
                            layer.msg('已退货', { icon: 6, time: 2000 });
                            window.location.reload();
                        }
                        else if (result == "false") {
                            $("[name ='orderChk']:checkbox").attr("checked", false);
                            layer.msg('退货失败！存在已不能再退货的订单', { icon: 5, time: 2000 });
                        }
                    },
                    error: function () {
                        layer.msg('网络错误！请稍后再试', { icon: 5, time: 2000 });
                    }
                });
            });
    }

    /*订单-删除*/
    function order_del(obj, url, id) {
        layer.confirm('确认要删除吗？', function (index) {
            $.ajax({
                type: 'POST',
                url: url,
                data: {
                    "orderid": id
                },
                success: function (response) {
                    if (response == "ok") {
                        $(obj).parents("tr").remove();
                        layer.msg('已删除!', { icon: 1, time: 2000 });
                    }
                    else
                        layer.msg('删除失败! ', { icon: 5, time: 3000 });
                },
                error: function () {
                    layer.msg('删除失败!', { icon: 5, time: 3000 });
                },
            });
        });
    }

    //订单批删除
    function order_batch_del(url) {
        var checkedNum = $("input[name='orderChk']:checked").length;
        if (checkedNum == 0) {
            layer.msg('请选择至少一项！', { icon: 1, time: 2000 });
            return;
        }
        layer.confirm('确定要删除所选项目？', function (index) {
            var checkedList = new Array();
            $("input[name='orderChk']:checked").each(function () {
                checkedList.push($(this).val());
            });
            $.ajax({
                type: "POST",
                url: url,
                data: { 'delitems': checkedList.toString() },
                success: function (result) {
                    if (result == "ok") {
                        $("input[name='orderChk']:checked").each(function () {
                            $(this).parent("tr").remove();
                        });
                        $("[name ='orderChk']:checkbox").attr("checked", false);
                        layer.msg('已删除!', { icon: 1, time: 2000 });
                        self.setInterval(function () {
                            window.location.reload();
                        }, 2000); 
                    }
                    else
                        layer.msg('存在无法删除的订单！', { icon: 5, time: 2000 });
                },
                error: function () {
                    layer.msg('删除失败! 请稍后再试', { icon: 5, time: 2000 });
                },
            });
        });
    }

    /*图书-查看界面*/
    function Book_show_by_name(title, url, w, h, name) {
        var url = url + '?name=' + name;
        layer_show(title, url, w, h);
    }
    /*用户-详情*/
    function user_show(title, url, w, h, Cid) {
        url = url + '?Cid=' + Cid;
        layer_show(title, url, w, h);
    }

</script>